﻿using System;

using DevExpress.Xpo;

using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using System.ComponentModel;
using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;

namespace SpeedSCM.Module
{

    [NavigationItem("库存管理")]
    [ImageName("BO_Order_Item")]
    public class 报溢单 : 单据
    {
        public 报溢单(Session session)
            : base(session)
        {

        }
        protected override SeedType SeedType
        {
            get { return SeedType.报溢单; }
        }

        private 仓库 _收货仓库;
        [Custom("Caption", "收货仓库")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择收货仓库")]
        public 仓库 收货仓库
        {
            get { return _收货仓库; }
            set { SetPropertyValue<仓库>("收货仓库", ref _收货仓库, value); }
        }

        [Association("报溢单-报溢单明细")]
        public XPCollection<报溢单明细> 报溢单明细
        {
            get { return GetCollection<报溢单明细>("报溢单明细"); }
        }
        public override bool Excute(IObjectSpace objectSpace)
        {
            foreach (报溢单明细 item in 报溢单明细)
            {
                库存 s = objectSpace.FindObject<库存>(CriteriaOperator.Parse("商品名称=? and 仓库名称=?", item.商品名称, 收货仓库));
                if (s == null)
                {
                    s = objectSpace.CreateObject<库存>();
                    s.商品名称 = item.商品名称;
                    s.仓库名称 = 收货仓库;
                    s.库存数量 = item.报溢商品数量;
                }
                else
                {
                    s.库存数量 += item.报溢商品数量;
                }
                s.Save();
            }
            objectSpace.CommitChanges();
            return base.Excute(objectSpace);
        }
    }
    [ImageName("BO_Order_Item")]
    [RuleCombinationOfPropertiesIsUnique(null, DefaultContexts.Save, "报溢单,商品名称", CustomMessageTemplate = "该报溢单已经存在该商品")]
    public class 报溢单明细 : BaseObject
    {
        public 报溢单明细(Session session)
            : base(session)
        {

        }
        private 报溢单 _报溢单;
        [Custom("Caption", "报溢单")]
        [Association("报溢单-报溢单明细")]
        [Custom("AllowEdit", "False")]
        public 报溢单 报溢单
        {
            get { return _报溢单; }
            set { SetPropertyValue<报溢单>("报溢单", ref _报溢单, value); }
        }
        private 商品 _商品名称;
        [Custom("Caption", "商品名称")]
        [RuleRequiredField(null, DefaultContexts.Save, "必须选择商品")]
        public 商品 商品名称
        {
            get { return _商品名称; }
            set { SetPropertyValue<商品>("商品名称", ref _商品名称, value); }
        }
        private decimal _报溢商品数量;
        [Custom("Caption", "报溢商品数量")]
        [Custom("EditMask", "n2")]
        [Custom("DisplayFormat", "{0:n2}")]
        [RuleRequiredField]
        public decimal 报溢商品数量
        {
            get { return _报溢商品数量; }
            set { SetPropertyValue<decimal>("报溢商品数量", ref _报溢商品数量, value); }
        }
        private string _备注;
        [Custom("Caption", "备注")]
        [Size(500)]
        public string 备注
        {
            get { return _备注; }
            set { SetPropertyValue<string>("备注", ref _备注, value); }
        }
    }
}
